﻿<?php
require('../database/db.php');
$mysql=new mysql();
error_reporting(0);
// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=10;
}
// 分页 end
// 需传参数
$shopid=$_REQUEST['shopid'];
$zixilie=$_REQUEST['ziid'];
// 需传参数 end

if ($shopid!='' ) {
	//1是销量 2是价格
	$sort = $_REQUEST['sort'];
	//-1降序 1升序
	$sort_type = $_REQUEST['sort_type'];

	//默认按照最新商品显示
	$sortSql = 'order by goodsid desc';
	if($sort != '' && $sort_type != ''){
		$key = $sort == 1 ? 'xiaoliang':'price';
		$val = $sort_type == -1 ?'desc':'asc';
		$sortSql = ' order by '.$key.' '.$val.' ';
	}

	//搜索
	$search = '';
	if($_REQUEST['search']){
		$search = ' and goods.title like "'.$_REQUEST['search'].'%"';
	}


	if ($zixilie != '' && $search == '') {
		$sql="select goodsid,title,images,price,yuanjia,xiaoliang from goods where shopid='".$shopid."' and zixilie='".$zixilie."' and number>0  ".$sortSql." limit $de,$size";
	}else{
		$sql="select goodsid,title,images,price,yuanjia,xiaoliang from goods where shopid='".$shopid."'   and number>0 ".$search.$sortSql." limit $de,$size";
	}
	$goods=$mysql->getAll($sql);
	$len=count($goods);
	for($i=0;$i<$len;$i++){
		// 设置默认图
		// $images = explode(',', $goods[$i]['images']);
		// if ($images[$goods[$i][defaultImgOrd]] !='') {
		// 	$defaultImg = $images[$goods[$i][defaultImgOrd]];
		// }else{
		// 	$defaultImg = $image[0];
		// }
		// $goods[$i][mainimages] = $defaultImg;
		// 设置默认图 end
		
		// $goodsid =$goods[$i][goodsid];
		// $sql2 = "select * from goodsselect where goodsid=$goodsid and number>0";
		// $goods[$i][goodsselect]=$mysql->getAll($sql2);

		$goods[$i][number] = $goods[$i][xiaoliang];
		$goods[$i][name] = $goods[$i][title];
		$goods[$i][intro] = $goods[$i][summary];
		$goods[$i][id] = $goods[$i][goodsid];
		$goods[$i][content] = 1;
		$goods[$i][qq] = $goods[$i][qq];

	}
	$arr = array('code' => 200, 'tip' =>'查询成功' ,'goods' =>$goods);
}else{
	$arr = array('code' => -100, 'tip' =>'参数传入有误');
}
echo json_encode($arr);
?>